package org.batallademusica.dominio;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.batallademusica.dao.DAOFactory;
import org.batallademusica.dao.DAOImplementations;
import org.batallademusica.dao.EntityManagerRepository;

public class GestorBatallaDeMusica {
	public void nuevaCancion(Cancion miCancion){
		EntityManager em=EntityManagerRepository.getInstance().recuperarEntityManager();
		try{
			EntityTransaction t=em.getTransaction();
			try{
				t.begin();
				DAOFactory.getInstance(DAOImplementations.JPA).getCancionDAO().crear(miCancion);
				t.commit();
			}finally{
				if(t.isActive()) t.rollback();
			}
		}finally{
			EntityManagerRepository.getInstance().cerrarEntityManager();
		}
	}
	
	public void borrarCancion(CancionPK llavePrimaria){
		EntityManager em=EntityManagerRepository.getInstance().recuperarEntityManager();
		try{
			EntityTransaction t=em.getTransaction();
			try{
				t.begin();
				DAOFactory.getInstance(DAOImplementations.JPA).getCancionDAO().borrar(llavePrimaria);
				t.commit();
			}finally{
				if(t.isActive()) t.rollback();
			}
		}finally{
			EntityManagerRepository.getInstance().cerrarEntityManager();
		}
	}
}
